package com.aboxes.mp3.mp3player;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.aboxes.R;
import com.aboxes.mp3.model.Mp3Info;
import com.aboxes.mp3.mp3player.service.AppConstant;
import com.aboxes.mp3.mp3player.service.PlayerService;
import com.aboxes.mp3.mp3player.service.AppConstant.PlayerMsg;

public class PlayerActivity extends Activity {

	ImageButton beginButton = null;
	ImageButton pauseButton = null;
	ImageButton stopButton = null;
	private boolean isPlaying = false;
	private boolean isPause = false;
	private boolean isReleased = false;
	private Mp3Info mp3Info = null;
	MediaPlayer mediaPlayer = null;

	private TextView lrcTextView = null;
	private IntentFilter intentFilter = null;
	private BroadcastReceiver receiver = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player);
		Intent intent = getIntent();
		mp3Info = (Mp3Info) intent.getSerializableExtra("mp3Info");
		beginButton = (ImageButton) findViewById(R.id.begin);
		pauseButton = (ImageButton) findViewById(R.id.pause);
		stopButton = (ImageButton) findViewById(R.id.stop);
		beginButton.setOnClickListener(new BeginButtonListener());
		pauseButton.setOnClickListener(new PauseButtonListener());
		stopButton.setOnClickListener(new StopButtonListener());
		lrcTextView = (TextView) findViewById(R.id.lrcText);

	}

	class StopButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("MSG", PlayerMsg.STOP_MSG);
			intent.setClass(PlayerActivity.this, PlayerService.class);
			startService(intent);

		}

	}

	class BeginButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("mp3Info", mp3Info);
			intent.putExtra("MSG", PlayerMsg.PLAY_MSG);
			intent.setClass(PlayerActivity.this, PlayerService.class);

			startService(intent);

		}

	}

	class PauseButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("MSG", PlayerMsg.PAUSE_MSG);
			intent.setClass(PlayerActivity.this, PlayerService.class);
			startService(intent);

		}
	}

	class LrcMessageBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String lrcMessage = intent.getStringExtra("lrcMessage");
			lrcTextView.setText(lrcMessage);

		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(receiver);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		receiver = new LrcMessageBroadcastReceiver();
		registerReceiver(receiver, getIntentFilter());
	}

	private IntentFilter getIntentFilter() {
		// TODO Auto-generated method stub
		if (intentFilter == null) {
			intentFilter = new IntentFilter();
			intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);
		}
		return intentFilter;
	}
}
